package b2.widget.friend
{
	import b2.config.*;
	import b2.net.*;
	import b2.player.*;
	import b2.scene.*;
	import b2.scene.fight.*;
	import b2.util.*;
	import b2.widget.*;
	import b2.widget.tab.*;
	
	import com.greensock.*;
	import com.smartfoxserver.v2.entities.*;
	import com.smartfoxserver.v2.entities.variables.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;
	
	public class FriendDialog extends Sprite
	{
		private var campIndex:int;
		private var friendId:int;
		
		public function FriendDialog(campIndex:int, friendId:int)
		{
			super();
			this.campIndex = campIndex;
			this.friendId = friendId;
			var friend:PlayerInfo = PlayerInfoManager.getInstance().players[friendId];
			//背景图片
			var RoomBgr:Class = ResourceHelper.getInstance().getClassByName("RoomBgr");
			var roomBgr:Sprite = new RoomBgr();
			addChild(roomBgr);
			//线框
			var lineRec:Sprite = new Sprite();
			lineRec.graphics.lineStyle(2, 0x27AAE1);
			lineRec.graphics.drawRoundRect(20, 55, 413, 130, 15, 15);
			lineRec.graphics.drawRoundRect(18, 201, 416, 230, 20, 20);
			lineRec.filters = [new DropShadowFilter(2, 44, 0x000000, 1, 2, 2, 1, 1, false)];
			roomBgr.addChild(lineRec);
			//线框色块
			var rrect:Sprite = new Sprite();
			rrect.graphics.beginFill(0x204B6B);
//			rrect.graphics.drawRoundRect(30, 17, 150, 21, 15, 15);
			rrect.graphics.drawRoundRect(20, 55, 413, 130, 15, 15);
			rrect.graphics.endFill();
			rrect.graphics.beginFill(0x27AAE1);
			rrect.graphics.drawRoundRect(20, 203, 413, 25, 15, 15);
//			rrect.graphics.drawRoundRect(20, 327, 413, 25, 15, 15);
			rrect.graphics.endFill();
			roomBgr.addChild(rrect);
			//左侧信息
			var campConfig:CampConfig = Config.campConfigs[campIndex];
//			//房间号
//			var roomNumText:TextField = new TextField();
//			roomNumText.selectable = true;
//        	roomNumText.autoSize = TextFieldAutoSize.LEFT;
//			roomNumText.defaultTextFormat = new TextFormat("微软雅黑", 12, 0xFFFFFF);
//			roomNumText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
//			roomNumText.htmlText = "<font color='#FFFF00'>房间号:</font> " + SmartFoxHelper.getInstance().sfs.lastJoinedRoom.name;
//			roomNumText.x = 40;
//			roomNumText.y = 17;
//			roomBgr.addChild(roomNumText);
			//规则名字
			var ruleNameText:TextField = new TextField();
			ruleNameText.selectable = false;
        	ruleNameText.autoSize = TextFieldAutoSize.LEFT;
			ruleNameText.defaultTextFormat = new TextFormat("微软雅黑", 17, 0xFFFF00);
			ruleNameText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			ruleNameText.text = "“攻击要塞” 奖励规则";
			ruleNameText.x = (roomBgr.width - ruleNameText.width) / 2;
			ruleNameText.y = 58;
			roomBgr.addChild(ruleNameText);
			//规则详情
			var ruleDescText:TextField = new TextField();
			ruleDescText.selectable = false;
			ruleDescText.wordWrap = true;
			ruleDescText.multiline = true;
			ruleDescText.width = 390;
        	ruleDescText.autoSize = TextFieldAutoSize.LEFT;
			ruleDescText.defaultTextFormat = new TextFormat("微软雅黑", 14, 0x000000);
			ruleDescText.filters = [new GlowFilter(0xFFFFFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			ruleDescText.htmlText = "战胜好友所获得的荣誉和勋章按照敌我之间的坦克战力差值进行计算。也就是说使用战力小的坦克出战能够获得更多的勋章哦！";
			ruleDescText.x = 30;
			ruleDescText.y = 85;
			roomBgr.addChild(ruleDescText);
			//随机战役
			var campNameText:TextField = new TextField();
			campNameText.selectable = false;
        	campNameText.autoSize = TextFieldAutoSize.LEFT;
			campNameText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0xFFFFFF);
			campNameText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			campNameText.x = 80;
			campNameText.y = 150;
			campNameText.htmlText = "<font color='#FFFF00'>随机战役:</font> <i>" + campConfig.name + "  </i>";
			roomBgr.addChild(campNameText);
			//剩余攻击机会
			var remainAttackText:TextField = new TextField();
			remainAttackText.selectable = false;
        	remainAttackText.autoSize = TextFieldAutoSize.LEFT;
			remainAttackText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0xFFFFFF);
			remainAttackText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			remainAttackText.x = 270;
			remainAttackText.y = 150;
			var remainAttackStr:String = "<font color='#FFFF00'>剩余攻击机会:</font> <i>";
			for (var i:int = 0; i < 3 - friend.attackedCount; i++)
				remainAttackStr += "●"
			remainAttackStr += "  </i>";
			remainAttackText.htmlText = remainAttackStr;
			roomBgr.addChild(remainAttackText);
			//敌军
			var opponentTitleText:TextField = new TextField();
			opponentTitleText.selectable = false;
        	opponentTitleText.autoSize = TextFieldAutoSize.LEFT;
			opponentTitleText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0xFFFFFF);
			opponentTitleText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			opponentTitleText.x = 40;
			opponentTitleText.y = 203;
			opponentTitleText.htmlText = "<font color='#FFFF00'>" + friend.nickname + "</font> 的要塞驻军";
			roomBgr.addChild(opponentTitleText);
			//开始游戏
			var StartButton:Class = ResourceHelper.getInstance().getClassByName("RoomStartButton");
			var startButton:BasicButton = new BasicButton(StartButton, "立即攻击", 40);
			startButton.x = 80;
			startButton.y = 452;
			roomBgr.addChild(startButton);
			//返回按钮
			var ReturnButton:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr0");
			var returnButton:BasicButton = new BasicButton(ReturnButton, "离开");
			returnButton.x = 320;
			returnButton.y = 462;
			roomBgr.addChild(returnButton);
			returnButton.addEventListener(MouseEvent.CLICK, onCloseButtonClick);
			//关闭按钮
			var CloseButton:Class = ResourceHelper.getInstance().getClassByName("CloseButton");
			var closeButton:BasicButton = new BasicButton(CloseButton);
			closeButton.x = roomBgr.width - 44;
			closeButton.y = 14;
			roomBgr.addChild(closeButton);
			closeButton.addEventListener(MouseEvent.CLICK, onCloseButtonClick);
			//显示敌军
			var friendTankItem:FriendTankItem = new FriendTankItem(friend.campTankId);
			friendTankItem.x = 30;
			friendTankItem.y = 240;
			friendTankItem.scaleX = 2;
			friendTankItem.scaleY = 2;
			roomBgr.addChild(friendTankItem);
			var count:int = 0;
			i = 0;
			while (i < friend.tankPackage.tankList.length && count < 6)
			{
				var tankId:int = friend.tankPackage.tankList[i];
				if (tankId != friend.campTankId)
				{
					friendTankItem = new FriendTankItem(tankId);
					friendTankItem.x = 190 + (count % 3) * (friendTankItem.width + 5);
					friendTankItem.y = 235 + int(count / 3) * 100;
					roomBgr.addChild(friendTankItem);
					count++;
				}
				i++;
			}
			for (i = count; i < 6; i++)
			{
				friendTankItem = new FriendTankItem(-1);
				friendTankItem.x = 190 + (i % 3) * (friendTankItem.width + 5);
				friendTankItem.y = 235 + int(i / 3) * 100;
				roomBgr.addChild(friendTankItem);
			}
			startButton.addEventListener(MouseEvent.CLICK, onStartButtonClick);
		}

		public function close():void
		{
			if (this.parent != null)
			{
				this.parent.removeChild(this);
			}
		}
		
		private function onCloseButtonClick(event:MouseEvent):void
		{
			close();
		}
		
		private function onStartButtonClick(event:MouseEvent):void
		{
			var friend:PlayerInfo = PlayerInfoManager.getInstance().players[friendId];
			if (friend.attackedCount >= 3)
			{
	        	var popMessage:PopMessage = new PopMessage(friend.nickname + "的要塞今日已经被攻击了3次，放过他吧( ⊙ o ⊙ )", 0xFF0000);
	        	WidgetManager.getInstance().popOut([popMessage], false, true);
			}
			else
			{
				SceneManager.getInstance().loadScene(FightScene.getInstance());
				FightScene.getInstance().startFriendCamp(campIndex, friendId);
			}
			close();
		}
		
	}
}